package com.wux.labs.integration.rabbitmq.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api("生产者接口")
@RestController
@RequestMapping("/producer")
public class ProducerController {
    @Autowired
    private StreamBridge bridge;

    @ApiOperation("向RabbitMQ发送数据")
    @PostMapping("/rabbitmq")
    public String sendToKafka(String message) {
        boolean status = bridge.send("rabbitChannel1", MessageBuilder.withPayload(message)
                // 配置文件中定义了 routingKeyExpression: headers.routingKey
                // 所以这里需要用设置key = routingKey的Header，指定routingKey = first
                .setHeader("routingKey", "first")
                .build());
        return "发送消息：" + message + "=====>" + status;
    }
}
